/******************************************************************************
//             INTEL CORPORATION PROPRIETARY INFORMATION
//  This software is supplied under the terms of a license agreement or
//  nondisclosure agreement with Intel Corporation and may not be copied
//  or disclosed except in accordance with the terms of that agreement.
//        Copyright (c) 2000-2001 Intel Corporation. All Rights Reserved.
//
//  Description:
//      Intel(R) JPEG library for X platform 
//
********************************************************************************/
#include "_ijx.h"

///////////////////////////////////////
//Not supported in this version
///////////////////////////////////////
#ifdef _MOTION_DIFF

extern const Ipp8u _nIZigZagIndex[64];

/******************************************************************************
// Name:             ijMotionDiffEncoder_MCU_First_16s_C1
// Description:      
// Input Arguments: 
// Output Arguments:
//                  
// Remarks:      
// Returns:         
******************************************************************************/
IJXRESULT ijMotionDiffEncoder_MCU_First_16s_C1 (IJBUFFER         *pIJXBuffer,
                                                IJBITTREAMSTATE  *pStream,
                                                IJENCODESTATE    *pEncodeState)
{
    IJXRESULT rtCode;
    Ipp16s *pMCUBuf          = pIJXBuffer->paMCUBufCur[0];
    Ipp16s *pCoefBuf         = pIJXBuffer->paCoeffBufCur[0];
    Ipp16u *pDCTQuantTable   = (Ipp16u *)(pEncodeState->paIdxDCTQuantTable[0]);
    Ipp32s *pDCTDequantTable = (Ipp32s *)(pEncodeState->paIdxDCTDequantTable[0]);

    ippiDCTQuantFwd_JPEG_16s_I(pMCUBuf, pDCTQuantTable);

    rtCode=ippiEncodeHuffman_Direct_JPEG_16s1u (pMCUBuf,
                                               pStream->pBitStream,  
                                               &(pStream->nUsedBufBitsLen),
                                               &(pEncodeState->dcPred[0]),
                                               pEncodeState->paIdxDCHuffTable[0],
                                               pEncodeState->paIdxACHuffTable[0]);

    ippiDCTQuantInv_JPEG_16s(pMCUBuf, pCoefBuf, pDCTDequantTable);
    pIJXBuffer->paCoeffBufCur[0]+=64;
    
    return rtCode;
}

/******************************************************************************
// Name:             ijMotionDiffEncoder_MCU_First_16s_C1
// Description:      Forward transform for MCU (FDCT).
// Input Arguments: 
// Output Arguments:
//                  
// Remarks:      
// Returns:         
******************************************************************************/
IJXRESULT ijMotionDiffEncoder_MCU_Refine_16s_C1 (IJBUFFER         *pIJXBuffer,
                                                 IJBITTREAMSTATE  *pStream,
                                                 IJENCODESTATE    *pEncodeState)
{
    IJXRESULT rtCode;
    Ipp16s *pMCUBuf         = pIJXBuffer->paMCUBufCur[0];
    Ipp16s *pCoefBuf        = pIJXBuffer->paCoeffBufCur[0];
    Ipp16u *pDCTQuantTable  = (Ipp16u *)(pEncodeState->paIdxDCTQuantTable[0]);
    Ipp32s *pDCTDequantTable= (Ipp32s *)(pEncodeState->paIdxDCTDequantTable[0]);

    ippiSubBlock_8x8_16s_I(pCoefBuf, pMCUBuf,1);
    ippiDCTQuantFwd_JPEG_16s_I(pMCUBuf, pDCTQuantTable);

    rtCode=ippiEncodeHuffman_Direct_JPEG_16s1u (pMCUBuf,
                                               pStream->pBitStream,  
                                               &(pStream->nUsedBufBitsLen),
                                               &(pEncodeState->dcPred[0]),
                                               pEncodeState->paIdxDCHuffTable[0],
                                               pEncodeState->paIdxACHuffTable[0]);

    ippiDCTQuantInv_JPEG_16s_I(pMCUBuf, pDCTDequantTable);
    ippiAddBlock_8x8_16s_I(pMCUBuf, pCoefBuf, 1);
    pIJXBuffer->paCoeffBufCur[0]+=64;
    
    return rtCode;
}

/******************************************************************************
// Name:             ijMotionDiffEncoder_MCU444_First_16s_C1
// Description:      Forward transform for MCU (FDCT).
// Input Arguments: 
// Output Arguments:
//                  
// Remarks:      
// Returns:         
******************************************************************************/
IJXRESULT ijMotionDiffEncoder_MCU444_First_16s_C3 (IJBUFFER         *pIJXBuffer,
                                                   IJBITTREAMSTATE  *pStream,
                                                   IJENCODESTATE    *pEncodeState)
{
    IJXRESULT rtCode;
    Ipp16s **ppMCUBuf         = (Ipp16s **)(pIJXBuffer->paMCUBufCur);
    Ipp16s **ppCoefBuf        = (Ipp16s **)(pIJXBuffer->paCoeffBufCur);
    Ipp16u **ppDCTQuantTable  = (Ipp16u **)(pEncodeState->paIdxDCTQuantTable);
    Ipp32s **ppDCTDequantTable= (Ipp32s **)(pEncodeState->paIdxDCTDequantTable);

    // Channel 1
    ippiDCTQuantFwd_JPEG_16s_I(ppMCUBuf[0], ppDCTQuantTable[0]);
    rtCode=ippiEncodeHuffman_Direct_JPEG_16s1u (ppMCUBuf[0],
                                               pStream->pBitStream,  
                                               &(pStream->nUsedBufBitsLen),
                                               &(pEncodeState->dcPred[0]),
                                               pEncodeState->paIdxDCHuffTable[0],
                                               pEncodeState->paIdxACHuffTable[0]);
    ippiDCTQuantInv_JPEG_16s(ppMCUBuf[0], ppCoefBuf[0], ppDCTDequantTable[0]);
    pIJXBuffer->paCoeffBufCur[0]+=64;

#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif

    // Channel 2
    ippiDCTQuantFwd_JPEG_16s_I(ppMCUBuf[1], ppDCTQuantTable[1]);
    rtCode=ippiEncodeHuffman_Direct_JPEG_16s1u (ppMCUBuf[1],
                                               pStream->pBitStream,  
                                               &(pStream->nUsedBufBitsLen),
                                               &(pEncodeState->dcPred[1]),
                                               pEncodeState->paIdxDCHuffTable[1],
                                               pEncodeState->paIdxACHuffTable[1]);
    ippiDCTQuantInv_JPEG_16s(ppMCUBuf[1], ppCoefBuf[1], ppDCTDequantTable[1]);
    pIJXBuffer->paCoeffBufCur[1]+=64;

#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif

    
    // Channel 3
    ippiDCTQuantFwd_JPEG_16s_I(ppMCUBuf[2], ppDCTQuantTable[2]);
    rtCode=ippiEncodeHuffman_Direct_JPEG_16s1u (ppMCUBuf[2],
                                               pStream->pBitStream,  
                                               &(pStream->nUsedBufBitsLen),
                                               &(pEncodeState->dcPred[2]),
                                               pEncodeState->paIdxDCHuffTable[2],
                                               pEncodeState->paIdxACHuffTable[2]);
    ippiDCTQuantInv_JPEG_16s(ppMCUBuf[2], ppCoefBuf[2], ppDCTDequantTable[2]);
    pIJXBuffer->paCoeffBufCur[2]+=64;

#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif


    return IJX_RTCODE_OK;
}

/******************************************************************************
// Name:             ijMotionDiffEncoder_MCU444_Refine_16s_C3
// Description:      Forward transform for MCU (FDCT).
// Input Arguments: 
// Output Arguments:
//                  
// Remarks:      
// Returns:         
******************************************************************************/
IJXRESULT ijMotionDiffEncoder_MCU444_Refine_16s_C3 (IJBUFFER         *pIJXBuffer,
                                                   IJBITTREAMSTATE  *pStream,
                                                   IJENCODESTATE    *pEncodeState)
{
    IJXRESULT rtCode;
    Ipp16s **ppMCUBuf         = (Ipp16s **)(pIJXBuffer->paMCUBufCur);
    Ipp16s **ppCoefBuf        = (Ipp16s **)(pIJXBuffer->paCoeffBufCur);
    Ipp16u **ppDCTQuantTable  = (Ipp16u **)(pEncodeState->paIdxDCTQuantTable);
    Ipp32s **ppDCTDequantTable= (Ipp32s **)(pEncodeState->paIdxDCTDequantTable);

    // Channel 1
    ippiSubBlock_8x8_16s_I(ppCoefBuf[0], ppMCUBuf[0],1);
    ippiDCTQuantFwd_JPEG_16s_I(ppMCUBuf[0], ppDCTQuantTable[0]);
    rtCode=ippiEncodeHuffman_Direct_JPEG_16s1u (ppMCUBuf[0],
                                               pStream->pBitStream,  
                                               &(pStream->nUsedBufBitsLen),
                                               &(pEncodeState->dcPred[0]),
                                               pEncodeState->paIdxDCHuffTable[0],
                                               pEncodeState->paIdxACHuffTable[0]);
    ippiDCTQuantInv_JPEG_16s_I(ppMCUBuf[0], ppDCTDequantTable[0]);
    ippiAddBlock_8x8_16s_I(ppMCUBuf[0], ppCoefBuf[0], 1);
    pIJXBuffer->paCoeffBufCur[0]+=64;
#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif

    // Channel 2
    ippiSubBlock_8x8_16s_I(ppCoefBuf[1], ppMCUBuf[1],1);
    ippiDCTQuantFwd_JPEG_16s_I(ppMCUBuf[1], ppDCTQuantTable[1]);
    rtCode=ippiEncodeHuffman_Direct_JPEG_16s1u (ppMCUBuf[1],
                                               pStream->pBitStream,  
                                               &(pStream->nUsedBufBitsLen),
                                               &(pEncodeState->dcPred[1]),
                                               pEncodeState->paIdxDCHuffTable[1],
                                               pEncodeState->paIdxACHuffTable[1]);
    ippiDCTQuantInv_JPEG_16s_I(ppMCUBuf[1], ppDCTDequantTable[1]);
    ippiAddBlock_8x8_16s_I(ppMCUBuf[1], ppCoefBuf[1], 1);
    pIJXBuffer->paCoeffBufCur[1]+=64;
#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif

    
    // Channel 3
    ippiSubBlock_8x8_16s_I(ppCoefBuf[2], ppMCUBuf[2],1);
    ippiDCTQuantFwd_JPEG_16s_I(ppMCUBuf[2], ppDCTQuantTable[2]);
    rtCode=ippiEncodeHuffman_Direct_JPEG_16s1u (ppMCUBuf[2],
                                               pStream->pBitStream,  
                                               &(pStream->nUsedBufBitsLen),
                                               &(pEncodeState->dcPred[2]),
                                               pEncodeState->paIdxDCHuffTable[2],
                                               pEncodeState->paIdxACHuffTable[2]);
    ippiDCTQuantInv_JPEG_16s_I(ppMCUBuf[2], ppDCTDequantTable[2]);
    ippiAddBlock_8x8_16s_I(ppMCUBuf[2], ppCoefBuf[2], 1);
    pIJXBuffer->paCoeffBufCur[2]+=64;
#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif


    return IJX_RTCODE_OK;
}

/******************************************************************************
// Name:             ijMotionDiffEncoder_MCU422_First_16s_C1
// Description:      Forward transform for MCU (FDCT).
// Input Arguments: 
// Output Arguments:
//                  
// Remarks:      
// Returns:         
******************************************************************************/
IJXRESULT ijMotionDiffEncoder_MCU422_First_16s_C3 (IJBUFFER         *pIJXBuffer,
                                                   IJBITTREAMSTATE  *pStream,
                                                   IJENCODESTATE    *pEncodeState)
{
    IJXRESULT rtCode;
    Ipp16s **ppMCUBuf         = (Ipp16s **)(pIJXBuffer->paMCUBufCur);
    Ipp16s **ppCoefBuf        = (Ipp16s **)(pIJXBuffer->paCoeffBufCur);
    Ipp16u **ppDCTQuantTable  = (Ipp16u **)(pEncodeState->paIdxDCTQuantTable);
    Ipp32s **ppDCTDequantTable= (Ipp32s **)(pEncodeState->paIdxDCTDequantTable);

    // Channel 1
    ippiDCTQuantFwd_JPEG_16s_I(ppMCUBuf[0], ppDCTQuantTable[0]);
    ippiDCTQuantFwd_JPEG_16s_I(&(ppMCUBuf[0][64]), ppDCTQuantTable[0]);

    rtCode=ippiEncodeHuffman_Direct_JPEG_16s1u (ppMCUBuf[0],
                                               pStream->pBitStream,  
                                               &(pStream->nUsedBufBitsLen),
                                               &(pEncodeState->dcPred[0]),
                                               pEncodeState->paIdxDCHuffTable[0],
                                               pEncodeState->paIdxACHuffTable[0]);
#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif
    rtCode=ippiEncodeHuffman_Direct_JPEG_16s1u (&(ppMCUBuf[0][64]),
                                               pStream->pBitStream,  
                                               &(pStream->nUsedBufBitsLen),
                                               &(pEncodeState->dcPred[0]),
                                               pEncodeState->paIdxDCHuffTable[0],
                                               pEncodeState->paIdxACHuffTable[0]);
#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif

    ippiDCTQuantInv_JPEG_16s(ppMCUBuf[0], ppCoefBuf[0], ppDCTDequantTable[0]);
    ippiDCTQuantInv_JPEG_16s(&(ppMCUBuf[0][64]), &(ppCoefBuf[0][64]), ppDCTDequantTable[0]);
    pIJXBuffer->paCoeffBufCur[0]+=128;

    // Channel 2
    ippiDCTQuantFwd_JPEG_16s_I(ppMCUBuf[1], ppDCTQuantTable[1]);
    rtCode=ippiEncodeHuffman_Direct_JPEG_16s1u (ppMCUBuf[1],
                                               pStream->pBitStream,  
                                               &(pStream->nUsedBufBitsLen),
                                               &(pEncodeState->dcPred[1]),
                                               pEncodeState->paIdxDCHuffTable[1],
                                               pEncodeState->paIdxACHuffTable[1]);
    ippiDCTQuantInv_JPEG_16s(ppMCUBuf[1], ppCoefBuf[1], ppDCTDequantTable[1]);
    pIJXBuffer->paCoeffBufCur[1]+=64;
#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif

    
    // Channel 3
    ippiDCTQuantFwd_JPEG_16s_I(ppMCUBuf[2], ppDCTQuantTable[2]);
    rtCode=ippiEncodeHuffman_Direct_JPEG_16s1u (ppMCUBuf[2],
                                               pStream->pBitStream,  
                                               &(pStream->nUsedBufBitsLen),
                                               &(pEncodeState->dcPred[2]),
                                               pEncodeState->paIdxDCHuffTable[2],
                                               pEncodeState->paIdxACHuffTable[2]);
    ippiDCTQuantInv_JPEG_16s(ppMCUBuf[2], ppCoefBuf[2], ppDCTDequantTable[2]);
    pIJXBuffer->paCoeffBufCur[2]+=64;

#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif


    return IJX_RTCODE_OK;
}

/******************************************************************************
// Name:             ijMotionDiffEncoder_MCU422_Refine_16s_C3
// Description:      Forward transform for MCU (FDCT).
// Input Arguments: 
// Output Arguments:
//                  
// Remarks:      
// Returns:         
******************************************************************************/
IJXRESULT ijMotionDiffEncoder_MCU422_Refine_16s_C3 (IJBUFFER         *pIJXBuffer,
                                                   IJBITTREAMSTATE  *pStream,
                                                   IJENCODESTATE    *pEncodeState)
{
    IJXRESULT rtCode;
    Ipp16s **ppMCUBuf         = (Ipp16s **)(pIJXBuffer->paMCUBufCur);
    Ipp16s **ppCoefBuf        = (Ipp16s **)(pIJXBuffer->paCoeffBufCur);
    Ipp16u **ppDCTQuantTable  = (Ipp16u **)(pEncodeState->paIdxDCTQuantTable);
    Ipp32s **ppDCTDequantTable= (Ipp32s **)(pEncodeState->paIdxDCTDequantTable);

    // Channel 1
    ippiSubBlock_8x8_16s_I(ppCoefBuf[0], ppMCUBuf[0],2);
    ippiDCTQuantFwd_JPEG_16s_I(ppMCUBuf[0], ppDCTQuantTable[0]);
    ippiDCTQuantFwd_JPEG_16s_I(&(ppMCUBuf[0][64]), ppDCTQuantTable[0]);
    rtCode=ippiEncodeHuffman_Direct_JPEG_16s1u (ppMCUBuf[0],
                                               pStream->pBitStream,  
                                               &(pStream->nUsedBufBitsLen),
                                               &(pEncodeState->dcPred[0]),
                                               pEncodeState->paIdxDCHuffTable[0],
                                               pEncodeState->paIdxACHuffTable[0]);
#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif
    rtCode=ippiEncodeHuffman_Direct_JPEG_16s1u (&(ppMCUBuf[0][64]),
                                               pStream->pBitStream,  
                                               &(pStream->nUsedBufBitsLen),
                                               &(pEncodeState->dcPred[0]),
                                               pEncodeState->paIdxDCHuffTable[0],
                                               pEncodeState->paIdxACHuffTable[0]);
#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif

    ippiDCTQuantInv_JPEG_16s_I(ppMCUBuf[0], ppDCTDequantTable[0]);
    ippiDCTQuantInv_JPEG_16s_I(&(ppMCUBuf[0][64]), ppDCTDequantTable[0]);
    ippiAddBlock_8x8_16s_I(ppMCUBuf[0], ppCoefBuf[0], 2);
    pIJXBuffer->paCoeffBufCur[0]+=128;

    // Channel 2
    ippiSubBlock_8x8_16s_I(ppCoefBuf[1], ppMCUBuf[1],1);
    ippiDCTQuantFwd_JPEG_16s_I(ppMCUBuf[1], ppDCTQuantTable[1]);
    rtCode=ippiEncodeHuffman_Direct_JPEG_16s1u (ppMCUBuf[1],
                                               pStream->pBitStream,  
                                               &(pStream->nUsedBufBitsLen),
                                               &(pEncodeState->dcPred[1]),
                                               pEncodeState->paIdxDCHuffTable[1],
                                               pEncodeState->paIdxACHuffTable[1]);
    ippiDCTQuantInv_JPEG_16s_I(ppMCUBuf[1], ppDCTDequantTable[1]);
    ippiAddBlock_8x8_16s_I(ppMCUBuf[1], ppCoefBuf[1], 1);
    pIJXBuffer->paCoeffBufCur[1]+=64;
#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif

    
    // Channel 3
    ippiSubBlock_8x8_16s_I(ppCoefBuf[2], ppMCUBuf[2],1);
    ippiDCTQuantFwd_JPEG_16s_I(ppMCUBuf[2], ppDCTQuantTable[2]);
    rtCode=ippiEncodeHuffman_Direct_JPEG_16s1u (ppMCUBuf[2],
                                               pStream->pBitStream,  
                                               &(pStream->nUsedBufBitsLen),
                                               &(pEncodeState->dcPred[2]),
                                               pEncodeState->paIdxDCHuffTable[2],
                                               pEncodeState->paIdxACHuffTable[2]);
    ippiDCTQuantInv_JPEG_16s_I(ppMCUBuf[2], ppDCTDequantTable[2]);
    ippiAddBlock_8x8_16s_I(ppMCUBuf[2], ppCoefBuf[2], 1);
    pIJXBuffer->paCoeffBufCur[2]+=64;
#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif


    return IJX_RTCODE_OK;
}

/******************************************************************************
// Name:             ijMotionDiffEncoder_MCU411_First_16s_C1
// Description:      Forward transform for MCU (FDCT).
// Input Arguments: 
// Output Arguments:
//                  
// Remarks:      
// Returns:         
******************************************************************************/
IJXRESULT ijMotionDiffEncoder_MCU411_First_16s_C3 (IJBUFFER         *pIJXBuffer,
                                                   IJBITTREAMSTATE  *pStream,
                                                   IJENCODESTATE    *pEncodeState)
{
    IJXRESULT rtCode;
    Ipp16s **ppMCUBuf         = (Ipp16s **)(pIJXBuffer->paMCUBufCur);
    Ipp16s **ppCoefBuf        = (Ipp16s **)(pIJXBuffer->paCoeffBufCur);
    Ipp16u **ppDCTQuantTable  = (Ipp16u **)(pEncodeState->paIdxDCTQuantTable);
    Ipp32s **ppDCTDequantTable= (Ipp32s **)(pEncodeState->paIdxDCTDequantTable);

    // Channel 1
    ippiDCTQuantFwd_JPEG_16s_I(ppMCUBuf[0], ppDCTQuantTable[0]);
    ippiDCTQuantFwd_JPEG_16s_I(&(ppMCUBuf[0][64]), ppDCTQuantTable[0]);
    ippiDCTQuantFwd_JPEG_16s_I(&(ppMCUBuf[0][128]), ppDCTQuantTable[0]);
    ippiDCTQuantFwd_JPEG_16s_I(&(ppMCUBuf[0][192]), ppDCTQuantTable[0]);

    rtCode=ippiEncodeHuffman_Direct_JPEG_16s1u (ppMCUBuf[0],
                                               pStream->pBitStream,  
                                               &(pStream->nUsedBufBitsLen),
                                               &(pEncodeState->dcPred[0]),
                                               pEncodeState->paIdxDCHuffTable[0],
                                               pEncodeState->paIdxACHuffTable[0]);
#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif

    rtCode=ippiEncodeHuffman_Direct_JPEG_16s1u (&(ppMCUBuf[0][64]),
                                               pStream->pBitStream,  
                                               &(pStream->nUsedBufBitsLen),
                                               &(pEncodeState->dcPred[0]),
                                               pEncodeState->paIdxDCHuffTable[0],
                                               pEncodeState->paIdxACHuffTable[0]);
#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif

    rtCode=ippiEncodeHuffman_Direct_JPEG_16s1u (&(ppMCUBuf[0][128]),
                                               pStream->pBitStream,  
                                               &(pStream->nUsedBufBitsLen),
                                               &(pEncodeState->dcPred[0]),
                                               pEncodeState->paIdxDCHuffTable[0],
                                               pEncodeState->paIdxACHuffTable[0]);
#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif

    rtCode=ippiEncodeHuffman_Direct_JPEG_16s1u (&(ppMCUBuf[0][192]),
                                               pStream->pBitStream,  
                                               &(pStream->nUsedBufBitsLen),
                                               &(pEncodeState->dcPred[0]),
                                               pEncodeState->paIdxDCHuffTable[0],
                                               pEncodeState->paIdxACHuffTable[0]);

#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif

    ippiDCTQuantInv_JPEG_16s(ppMCUBuf[0], ppCoefBuf[0], ppDCTDequantTable[0]);
    ippiDCTQuantInv_JPEG_16s(&(ppMCUBuf[0][64]), &(ppCoefBuf[0][64]), ppDCTDequantTable[0]);
    ippiDCTQuantInv_JPEG_16s(&(ppMCUBuf[0][128]), &(ppCoefBuf[0][128]), ppDCTDequantTable[0]);
    ippiDCTQuantInv_JPEG_16s(&(ppMCUBuf[0][192]), &(ppCoefBuf[0][192]), ppDCTDequantTable[0]);
    pIJXBuffer->paCoeffBufCur[0]+=256;

    // Channel 2
    ippiDCTQuantFwd_JPEG_16s_I(ppMCUBuf[1], ppDCTQuantTable[1]);
    rtCode=ippiEncodeHuffman_Direct_JPEG_16s1u (ppMCUBuf[1],
                                               pStream->pBitStream,  
                                               &(pStream->nUsedBufBitsLen),
                                               &(pEncodeState->dcPred[1]),
                                               pEncodeState->paIdxDCHuffTable[1],
                                               pEncodeState->paIdxACHuffTable[1]);
    ippiDCTQuantInv_JPEG_16s(ppMCUBuf[1], ppCoefBuf[1], ppDCTDequantTable[1]);
    pIJXBuffer->paCoeffBufCur[1]+=64;
#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif

    
    // Channel 3
    ippiDCTQuantFwd_JPEG_16s_I(ppMCUBuf[2], ppDCTQuantTable[2]);
    rtCode=ippiEncodeHuffman_Direct_JPEG_16s1u (ppMCUBuf[2],
                                               pStream->pBitStream,  
                                               &(pStream->nUsedBufBitsLen),
                                               &(pEncodeState->dcPred[2]),
                                               pEncodeState->paIdxDCHuffTable[2],
                                               pEncodeState->paIdxACHuffTable[2]);
    ippiDCTQuantInv_JPEG_16s(ppMCUBuf[2], ppCoefBuf[2], ppDCTDequantTable[2]);
    pIJXBuffer->paCoeffBufCur[2]+=64;

#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif


    return IJX_RTCODE_OK;
}
/*
void display_8x8_Ipp16s (Ipp16s *pSrc)
{
    int x,y;

    printf ("\n");
    for (y=0; y<8; y++) {
        for (x=0; x<8; x++) {
            printf ("%d, ", pSrc[x]);
        }
        pSrc+=8;
        printf ("\n");
    }
}
*/
/******************************************************************************
// Name:             ijMotionDiffEncoder_MCU411_Refine_16s_C3
// Description:      Forward transform for MCU (FDCT).
// Input Arguments: 
// Output Arguments:
//                  
// Remarks:      
// Returns:         
******************************************************************************/
IJXRESULT ijMotionDiffEncoder_MCU411_Refine_16s_C3 (IJBUFFER         *pIJXBuffer,
                                                   IJBITTREAMSTATE  *pStream,
                                                   IJENCODESTATE    *pEncodeState)
{
    IJXRESULT rtCode;
    Ipp16s **ppMCUBuf         = (Ipp16s **)(pIJXBuffer->paMCUBufCur);
    Ipp16s **ppCoefBuf        = (Ipp16s **)(pIJXBuffer->paCoeffBufCur);
    Ipp16u **ppDCTQuantTable  = (Ipp16u **)(pEncodeState->paIdxDCTQuantTable);
    Ipp32s **ppDCTDequantTable= (Ipp32s **)(pEncodeState->paIdxDCTDequantTable);

    // Channel 1
    ippiSubBlock_8x8_16s_I(ppCoefBuf[0], ppMCUBuf[0],4);
    //memset(ppMCUBuf[0],0, 64*4);
    ippiDCTQuantFwd_JPEG_16s_I(ppMCUBuf[0], ppDCTQuantTable[0]);
    ippiDCTQuantFwd_JPEG_16s_I(&(ppMCUBuf[0][64]), ppDCTQuantTable[0]);
    ippiDCTQuantFwd_JPEG_16s_I(&(ppMCUBuf[0][128]), ppDCTQuantTable[0]);
    ippiDCTQuantFwd_JPEG_16s_I(&(ppMCUBuf[0][192]), ppDCTQuantTable[0]);
    rtCode=ippiEncodeHuffman_Direct_JPEG_16s1u (ppMCUBuf[0],
                                               pStream->pBitStream,  
                                               &(pStream->nUsedBufBitsLen),
                                               &(pEncodeState->dcPred[0]),
                                               pEncodeState->paIdxDCHuffTable[0],
                                               pEncodeState->paIdxACHuffTable[0]);
#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif
    rtCode=ippiEncodeHuffman_Direct_JPEG_16s1u (&(ppMCUBuf[0][64]),
                                               pStream->pBitStream,  
                                               &(pStream->nUsedBufBitsLen),
                                               &(pEncodeState->dcPred[0]),
                                               pEncodeState->paIdxDCHuffTable[0],
                                               pEncodeState->paIdxACHuffTable[0]);
#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif

    rtCode=ippiEncodeHuffman_Direct_JPEG_16s1u (&(ppMCUBuf[0][128]),
                                               pStream->pBitStream,  
                                               &(pStream->nUsedBufBitsLen),
                                               &(pEncodeState->dcPred[0]),
                                               pEncodeState->paIdxDCHuffTable[0],
                                               pEncodeState->paIdxACHuffTable[0]);
#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif

    rtCode=ippiEncodeHuffman_Direct_JPEG_16s1u (&(ppMCUBuf[0][192]),
                                               pStream->pBitStream,  
                                               &(pStream->nUsedBufBitsLen),
                                               &(pEncodeState->dcPred[0]),
                                               pEncodeState->paIdxDCHuffTable[0],
                                               pEncodeState->paIdxACHuffTable[0]);
#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif

    ippiDCTQuantInv_JPEG_16s_I(ppMCUBuf[0], ppDCTDequantTable[0]);
    ippiDCTQuantInv_JPEG_16s_I(&(ppMCUBuf[0][64]), ppDCTDequantTable[0]);
    ippiDCTQuantInv_JPEG_16s_I(&(ppMCUBuf[0][128]), ppDCTDequantTable[0]);
    ippiDCTQuantInv_JPEG_16s_I(&(ppMCUBuf[0][192]), ppDCTDequantTable[0]);
    ippiAddBlock_8x8_16s_I(ppMCUBuf[0], ppCoefBuf[0], 4);
    pIJXBuffer->paCoeffBufCur[0]+=256;

    // Channel 2
    ippiSubBlock_8x8_16s_I(ppCoefBuf[1], ppMCUBuf[1],1);
    //memset(ppMCUBuf[1],0, 64);
    ippiDCTQuantFwd_JPEG_16s_I(ppMCUBuf[1], ppDCTQuantTable[1]);
    rtCode=ippiEncodeHuffman_Direct_JPEG_16s1u (ppMCUBuf[1],
                                               pStream->pBitStream,  
                                               &(pStream->nUsedBufBitsLen),
                                               &(pEncodeState->dcPred[1]),
                                               pEncodeState->paIdxDCHuffTable[1],
                                               pEncodeState->paIdxACHuffTable[1]);
    ippiDCTQuantInv_JPEG_16s_I(ppMCUBuf[1], ppDCTDequantTable[1]);
    ippiAddBlock_8x8_16s_I(ppMCUBuf[1], ppCoefBuf[1], 1);
    pIJXBuffer->paCoeffBufCur[1]+=64;
#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif

    
    // Channel 3
    ippiSubBlock_8x8_16s_I(ppCoefBuf[2], ppMCUBuf[2],1);
    //memset(ppMCUBuf[2],0, 64);
    ippiDCTQuantFwd_JPEG_16s_I(ppMCUBuf[2], ppDCTQuantTable[2]);
    rtCode=ippiEncodeHuffman_Direct_JPEG_16s1u (ppMCUBuf[2],
                                               pStream->pBitStream,  
                                               &(pStream->nUsedBufBitsLen),
                                               &(pEncodeState->dcPred[2]),
                                               pEncodeState->paIdxDCHuffTable[2],
                                               pEncodeState->paIdxACHuffTable[2]);
    ippiDCTQuantInv_JPEG_16s_I(ppMCUBuf[2], ppDCTDequantTable[2]);
    ippiAddBlock_8x8_16s_I(ppMCUBuf[2], ppCoefBuf[2], 1);
    pIJXBuffer->paCoeffBufCur[2]+=64;
#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif


    return IJX_RTCODE_OK;
}

#endif
///////////////////////////////////////

/* EOF */
